package com.ruoyi.mushcoin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.mushcoin.domain.TgGameGoodsInfo;
import com.ruoyi.mushcoin.domain.bo.TgGameGoodsInfoBo;
import com.ruoyi.mushcoin.domain.table.TgGameGoodsInfoTableDef;
import com.ruoyi.mushcoin.domain.vo.TgGameGoodsInfoVo;
import com.ruoyi.mushcoin.mapper.TgGameGoodsInfoMapper;
import com.ruoyi.mushcoin.service.ITgGameGoodsInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

import static com.ruoyi.mushcoin.domain.table.TgGameGoodsInfoTableDef.TG_GAME_GOODS_INFO;

/**
 * TG游戏商品Service业务层处理
 *
 * @author billy
 * 2024-10-30
 */
@Service
public class TgGameGoodsInfoServiceImpl extends BaseServiceImpl<TgGameGoodsInfoMapper, TgGameGoodsInfo> implements ITgGameGoodsInfoService {
    @Resource
    private TgGameGoodsInfoMapper tgGameGoodsInfoMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(TG_GAME_GOODS_INFO);
    }

    private QueryWrapper buildQueryWrapper(TgGameGoodsInfoBo tgGameGoodsInfoBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TG_GAME_GOODS_INFO.GOODS_ID.eq(tgGameGoodsInfoBo.getGoodsId()));

        return queryWrapper;
    }

    /**
     * 查询TG游戏商品
     *
     * @param id TG游戏商品主键
     * @return TG游戏商品
     */
    @Override
    public TgGameGoodsInfoVo selectById(Long id) {
        return this.getOneAs(query().where(TG_GAME_GOODS_INFO.ID.eq(id)), TgGameGoodsInfoVo.class);

    }

    /**
     * 查询TG游戏商品列表
     *
     * @param tgGameGoodsInfoBo TG游戏商品Bo
     * @return TG游戏商品集合
     */
    @Override
    public List<TgGameGoodsInfoVo> selectList(TgGameGoodsInfoBo tgGameGoodsInfoBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgGameGoodsInfoBo);
        return this.listAs(queryWrapper, TgGameGoodsInfoVo.class);
    }

    /**
     * 分页查询TG游戏商品列表
     *
     * @param tgGameGoodsInfoBo TG游戏商品Bo
     * @return 分页TG游戏商品集合
     */
    @Override
    public TableDataInfo<TgGameGoodsInfoVo> selectPage(TgGameGoodsInfoBo tgGameGoodsInfoBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgGameGoodsInfoBo);
        Page<TgGameGoodsInfoVo> page = this.pageAs(PageQuery.build(), queryWrapper, TgGameGoodsInfoVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增TG游戏商品
     *
     * @param tgGameGoodsInfoBo TG游戏商品Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TgGameGoodsInfoBo tgGameGoodsInfoBo) {
        TgGameGoodsInfo tgGameGoodsInfo = MapstructUtils.convert(tgGameGoodsInfoBo, TgGameGoodsInfo.class);


        return this.save(tgGameGoodsInfo);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增TG游戏商品，前台提供主键值，一般用于导入的场合
     *
     * @param tgGameGoodsInfoBo TG游戏商品Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(TgGameGoodsInfoBo tgGameGoodsInfoBo) {
        TgGameGoodsInfo tgGameGoodsInfo = MapstructUtils.convert(tgGameGoodsInfoBo, TgGameGoodsInfo.class);


        return tgGameGoodsInfoMapper.insertWithPk(tgGameGoodsInfo) > 0;//前台传来主键值
    }

    /**
     * 修改TG游戏商品
     *
     * @param tgGameGoodsInfoBo TG游戏商品Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TgGameGoodsInfoBo tgGameGoodsInfoBo) {
        TgGameGoodsInfo tgGameGoodsInfo = MapstructUtils.convert(tgGameGoodsInfoBo, TgGameGoodsInfo.class);
        if (ObjectUtil.isNotNull(tgGameGoodsInfo) && ObjectUtil.isNotNull(tgGameGoodsInfo.getId())) {
            boolean updated = this.updateById(tgGameGoodsInfo);
            return updated;
        }
        return false;
    }


    /**
     * 批量删除TG游戏商品
     *
     * @param ids 需要删除的TG游戏商品主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }


    public TgGameGoodsInfo getByGoodsId(Long goodsId) {
        QueryWrapper query = new QueryWrapper();
        query.where(TgGameGoodsInfoTableDef.TG_GAME_GOODS_INFO.GOODS_ID.eq(goodsId));
        return this.getOne(query);
    }
}
